<!--
  * 进件查询
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <!-- <a-col :span="4">
        <a-form-item label="商户名称" class="smart-query-form-item">
          <CustomSelect
            v-model:value="queryForm.merchantId"
            :placeholder="'请选择'"
            :api="merchantApi.page"
            :queryParams="merchantParams"
            queryParamKey="companyNameLeftLike"
            valueField="id"
            labelField="companyName"
          />
        </a-form-item>
      </a-col> -->
      <a-col :span="4">
        <a-form-item label="商户名称" class="smart-query-form-item">
          <a-input v-model:value="queryForm.merchantName" placeholder="商户名称" />
        </a-form-item>
      </a-col>
      <a-col :span="4">
        <a-form-item label="支付通道" class="smart-query-form-item">
          <CustomSelect
            v-model:value="queryForm.channelId"
            :placeholder="'请选择支付通道'"
            :api="channelApi.queryPage"
            value-field="id"
            label-field="channelName"
          />
        </a-form-item>
      </a-col>
      <a-col :span="5">
        <a-form-item class="smart-query-form-item">
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery" class="smart-margin-left10">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-form-item>
      </a-col>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <div class="smart-table-title">数据列表</div>
    <a-row class="smart-table-btn-block">
      <div class="smart-table-setting-block">
        <!-- <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.OPERATION.ORDER" :refresh="queryData" /> -->
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ record, column }">
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="viewList(record)" type="link">查看产品</a-button>
            <!-- <a-button @click="refundForm(record)" v-if="record.channelType == 2" type="link">详情</a-button> -->
          </div>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>

  <ProductList v-model:open="productListOptions.open" :rowData="productListOptions.rowData" />
</template>
<script setup>
  import { onMounted, reactive, ref, getCurrentInstance } from 'vue'
  import { message } from 'ant-design-vue'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const'
  import { smartSentry } from '/@/lib/smart-sentry'
  import TableOperator from '/@/components/support/table-operator/index.vue'
  import { registApi } from '/@/api/business/b-operation/regist/index.js'
  import { channelApi } from '/@/api/business/b-operation/channel/channel-api'
  import { merchantApi } from '/@/api/business/b-operation/merchant/merchant-api.js'
  import CustomSelect from '/@/components/business/comment/CustomSelect.vue'
  import ProductList from '/@/components/business/comment/regist/product-list.vue'
  import dayjs from 'dayjs'
  import {
    REGIST_STATUS, //审核状态
  } from '/@/constants/dict'
  import { selectDictLabel } from '/@/utils/common'
  const { proxy } = getCurrentInstance()

  // ---------------------------- 表格列 ----------------------------

  const columns = ref([
    {
      title: '序号',
      dataIndex: 'index',
      width: 60,
      customRender: ({ index }) => {
        return (queryForm.pageNo - 1) * queryForm.pageSize + index + 1
      },
    },
    {
      title: '商户名称',
      dataIndex: 'merchantName',
      ellipsis: true,
    },
    {
      title: '商户联系人',
      dataIndex: 'id',
      ellipsis: true,
    },
    {
      title: '所属代理',
      dataIndex: 'oAgentName',
      ellipsis: true,
    },
    {
      title: '支付通道',
      dataIndex: 'channelName',
      ellipsis: true,
    },
    {
      title: '通道类型',
      dataIndex: 'channelType',
      customRender: ({ text }) => {
        return proxy.$smartEnumPlugin.getDescByValue('ZZB_CHANNEL_TYPE_ENUM', text)
        // return getBusinessName(record.businessType) || '未知'
      },
      ellipsis: true,
    },
    {
      title: '进件审核状态',
      dataIndex: 'status',
      customRender: ({ text }) => {
        return selectDictLabel(REGIST_STATUS, text)
      },
      ellipsis: true,
    },
    {
      title: '签约账户',
      dataIndex: 'account',
      ellipsis: true,
    },
    // {
    //   title: '结算卡账户',
    //   dataIndex: 'cleanAccount',
    //   ellipsis: true,
    // },
    // {
    //   title: '审核失败原因',
    //   dataIndex: 'reason',
    //   ellipsis: true,
    // },
    // {
    //   title: '创建时间',
    //   dataIndex: 'cTime',
    //   customRender: (column) => dayjs(column.text).format('YYYY-MM-DD HH:mm:ss'),
    //   width: 180,
    //   ellipsis: true,
    // },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 90,
    },
  ])

  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    fTime: dayjs().startOf('day'),
    fTimeBegin: dayjs().startOf('day').valueOf(), //前一天
    fTimeEnd: dayjs().endOf('day').valueOf(), //前一天
    merchantName: undefined,
    channelId: undefined,
    pageNo: 1,
    pageSize: 10,
  }
  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize
    Object.assign(queryForm, queryFormState)
    queryForm.pageSize = pageSize
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.pageNo = 1
    queryData()
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      let queryResult = await registApi.page(queryForm)
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }

  // 查看产品
  const productListOptions = ref({
    open: false,
    rowData: undefined,
  })
  function viewList(row) {
    productListOptions.value.open = true
    productListOptions.value.rowData = row
  }

  onMounted(queryData)
</script>
